package com.dzt.androidframework.database.tasks;

import android.app.Activity;
import android.support.annotation.NonNull;

import com.dzt.androidframework.constant.AppConstant;
import com.dzt.androidframework.database.addedittask.AddEditActivity;
import com.dzt.androidframework.database.javabean.Task;
import com.dzt.androidframework.utils.JLogKit;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by M02323 on 2017/6/10.
 */

public class TasksPresenter implements TasksContract.Presenter{

	private final TasksLocalDataSource localDataSource;
	private final TasksContract.View tasksView;
	private TasksFilterType currentFiltering = TasksFilterType.ALL_TASKS;
	private boolean firstLoad = true;
	
	public TasksPresenter(TasksLocalDataSource localDataSource, TasksContract.View tasksView){
		this.localDataSource = localDataSource;
		this.tasksView = tasksView;
	}
	
	@Override
	public void start() {
		loadTasks(false);
	}

	@Override
	public void result(int requestCode, int resultCode) {
		//添加任务返回到列表界面需要重新加载
		if(resultCode == Activity.RESULT_OK){
			if(requestCode == AppConstant.REQUEST_ADD_TASK
					|| requestCode == AppConstant.REQUEST_DETAIL_TASK){
				tasksView.reLoadTask();
			}
		}
	}

	@Override
	public void loadTasks(boolean forceUpdate) {
		loadTasks(forceUpdate || firstLoad, true);
		firstLoad = false;
	}

	/**
	 * @param forceUpdate   Pass in true to refresh the data in the {@link }
	 * @param showLoadingUI Pass in true to display a loading icon in the UI
	 */
	private void loadTasks(boolean forceUpdate, final boolean showLoadingUI) {
		JLogKit.getInstance().i("loadTasks forceUpdate = " + forceUpdate);
		if (showLoadingUI) {
			tasksView.setLoadingIndicator(true);
		}

		localDataSource.getTasks(new TasksDataSource.LoadTasksCallback() {
			@Override
			public void onTasksLoaded(List<Task> tasks) {
				List<Task> tasksToShow = new ArrayList<>();

				// We filter the tasks based on the requestType
				for (Task task : tasks) {
					switch (currentFiltering) {
						case ALL_TASKS:
							tasksToShow.add(task);
							break;
						case ACTIVE_TASKS:
							if (task.isActive()) {
								tasksToShow.add(task);
							}
							break;
						case COMPLETED_TASKS:
							if (task.isCompleted()) {
								tasksToShow.add(task);
							}
							break;
						default:
							tasksToShow.add(task);
							break;
					}
				}
				// The view may not be able to handle UI updates anymore
				if (!tasksView.isActive()) {
					return;
				}
				if (showLoadingUI) {
					tasksView.setLoadingIndicator(false);
				}

				processTasks(tasksToShow);
			}

			@Override
			public void onDataNotAvailable() {
				// The view may not be able to handle UI updates anymore
				if (!tasksView.isActive()) {
					return;
				}
				tasksView.showLoadingTasksError();
			}
		});
	}

	private void processTasks(List<Task> tasks) {
		if (tasks == null || tasks.isEmpty()) {
			// Show a message indicating there are no tasks for that filter type.
			processEmptyTasks();
		} else {
			// Show the list of tasks
			tasksView.showTasks(tasks);
			// Set the filter label's text.
			showFilterLabel();
		}
	}

	private void showFilterLabel() {
		switch (currentFiltering) {
			case ACTIVE_TASKS:
				tasksView.showActiveFilterLabel();
				break;
			case COMPLETED_TASKS:
				tasksView.showCompletedFilterLabel();
				break;
			default:
				tasksView.showAllFilterLabel();
				break;
		}
	}

	private void processEmptyTasks() {
		switch (currentFiltering) {
			case ACTIVE_TASKS:
				tasksView.showNoActiveTasks();
				break;
			case COMPLETED_TASKS:
				tasksView.showNoCompletedTasks();
				break;
			default:
				tasksView.showNoTasks();
				break;
		}
	}
	
	@Override
	public void addNewTask() {
		tasksView.showAddTask();
	}

	@Override
	public void openTaskDetails(@NonNull Task requestedTask) {
		tasksView.showTaskDetailsUi(requestedTask.getId());
	}

	@Override
	public void completeTask(@NonNull Task completedTask) {
		localDataSource.completeTask(completedTask);
		tasksView.showTaskMarkedComplete();
		loadTasks(false, false);
	}

	@Override
	public void activateTask(@NonNull Task activeTask) {
		localDataSource.activateTask(activeTask);
		tasksView.showTaskMarkedActive();
		loadTasks(false, false);
	}

	@Override
	public void clearCompletedTasks() {
		localDataSource.clearCompletedTasks();
		tasksView.showCompletedTasksCleared();
		loadTasks(false, false);
	}

	@Override
	public void setFiltering(TasksFilterType requestType) {
		currentFiltering = requestType;
	}

	@Override
	public TasksFilterType getFiltering() {
		return currentFiltering;
	}
}
